﻿/******************************************************************
** 文 件 名: Sys_Notepad_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-6-14 16:10:34
** 描    述: 记事本编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;


namespace Honestysoft.Financing.WebUI.Module.OtherBu
{
	public partial class Sys_Notepad_Edit : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {
				BindControl();
                Bind();
            }
        }
		
		private void BindControl()
		{
            //ControlHelper.BindEnum(ddlNoteType, "NoteType");//此处最好改成typeof(EnumItem.XXX)
		}

        private void Bind()
        {
			Sys_Notepad obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj = new BaseBusinessLogic<Sys_Notepad>().GetEntity(Request.QueryString["ID"]);
                //txtID.Text = obj.ID;
                hidIsPrivate.Value = obj.IsPrivate ? "1" : "0";
                hidNoteType.Value = obj.NoteType;
                hidCreateTime.Value = obj.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
                hidCreator.Value = obj.Creator;
                lblCreatorName.Text = obj.CreatorName;
                dtpNoteDate.Text = obj.NoteDate.ToString("yyyy-MM-dd");
                txtDescription.Text = obj.Description;
            }
            else
            {
                hidIsPrivate.Value =  "0";
                hidNoteType.Value = ((int)EnumItem.NoteType.AuditCase).ToString();
                hidCreateTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                hidCreator.Value = UserLoginer.LoginUser.ID;
                lblCreatorName.Text = UserLoginer.LoginUser.Name;
                dtpNoteDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }
		
		protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {            
            try
            {
                Sys_Notepad obj = GetSubmitObject();
                int ret = new BaseBusinessLogic<Sys_Notepad>().CreateEntity(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {            
            try
            {
                Sys_Notepad obj = GetSubmitObject();
                int ret = new BaseBusinessLogic<Sys_Notepad>().UpdateEntity(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
		/// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private Sys_Notepad GetSubmitObject()
        {
            Sys_Notepad obj = new Sys_Notepad();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
				obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
            obj.IsPrivate = hidIsPrivate.Value == "1";
			obj.NoteType = hidNoteType.Value;
            if (hidCreateTime.Value.Trim().Length > 0) { obj.CreateTime = Convert.ToDateTime(hidCreateTime.Value); }
            obj.Creator = hidCreator.Value;
			obj.CreatorName = lblCreatorName.Text;
			if(dtpNoteDate.Text.Trim().Length > 0){obj.NoteDate = Convert.ToDateTime(dtpNoteDate.Text);}
			obj.Description = txtDescription.Text;
            return obj;
        }
	}
}